import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  {
    path: '/index',
    name: 'index',
    meta:{title:'index'},
    component: ()=>import('../views/Index.vue'),
    children:[
      {
        path: '/index/home',
        name: 'home',
        meta:{title:'首页'},
        component: ()=>import('../views/Index/Home.vue'),
      },
      {
        path: '/index/tuan',
        name: 'tuan',
        meta:{title:'爆爆团'},
        component: ()=>import('../views/Index/Tuan.vue'),
      },
      {
        path: '/index/order',
        name: 'order',
        meta:{title:'订单'},
        component: ()=>import('../views/Index/Order.vue'),
      },
      {
        path: '/index/mine',
        name: 'mine',
        meta:{title:'我的'},
        component: ()=>import('../views/Index/Mine.vue'),
      },
      {
        path:'/index',
        redirect:'/index/home'
      },//二级路由的重定向
      {
        path:'*',
        meta:{title:'NotFound'},
        component:()=> import('../views/NotFound.vue')
      }
    ]
  },
  {
    path:'/detail/:id',
    name:'detail',
    meta:{title:'详情页'},
    component:()=>import('../views/Detail.vue')
  },
  {
    path: '/register',
    name: 'register',
    meta:{title:'注册'},
    component: ()=>import('../views/Register.vue'),
  },
  {
    path: '/login',
    name: 'login',
    meta:{title:'登录'},
    component: ()=>import('../views/Login.vue'),
  },
  {
    path: '/collect',
    name: 'collect',
    meta:{title:'收藏'},
    component: ()=>import('../views/Collect.vue'),
  },
  {
    path: '/search',
    name: 'search',
    meta:{title:'搜索'},
    component: ()=>import('../views/Search.vue'),
  },
  {
    path: '/address',
    name: 'address',
    meta:{title:'我的收货地址'},
    component: ()=>import('../views/Address.vue'),
  },
  {
    path: '/edit',
    name: 'edit',
    meta:{title:'新增地址'},
    component: ()=>import('../views/Edit.vue'),
  },
  {
    path:'/',
    redirect:'/index'
  },//一级路由的重定向
  {
    path:'*',
    meta:{title:'NotFound'},
    component:()=> import('../views/NotFound.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to,from,next)=>{
  document.title = to.meta.title;
  var token = localStorage.getItem('token');
  if(to.path=='/index/order'||to.path == '/index/mine'){
    if(token){
      next();
    }else{
      next('/login');
    }
  }else{
    next();
  }
})
export default router
